--Copyright © 1987 Stephen Michel 1027 Pomona, Albany, CA 94706
--9-3-87
--More notes at the end of the script
on mouseUp
if the optionkey is down then pass mouseup
global inFile
global error
newStack
show message box at 33,296
getFile
if error is not empty then
answer "Import Stopped!" with "Okay"
exit mouseUp
end if
goodFile
if error is not empty then
exit mouseUp
end if
createStruct
importText
choose browse tool
end mouseUp
on newStack
global inFile
global error
-- Creates the new stack
doMenu New Stack...
end newStack
on getFile
global inFile
global error
-- finds the file to import from
put FileName("TEXT") into it
if it is empty then
put "true" into error
else
put empty into error
put it into inFile
end if
end getFile
on createStruct
-- right now, this just creates a structure for a tab-delimited file
global inFile
global error
global countFields
put 0 into countFields
open file inFile
put "Now analyzing the structure & counting fields..."
put "dummy" into it
-- simply loops through the first record until it hits a return
-- and adds one to countFields every time it finds a tab
repeat until it is empty
read from file inFile until tab
add 1 to countFields
if it contains return then
exit repeat
end if
end repeat
close file inFile
put "There are " & countFields & " fields in the file"
domenu "background"
open file inFile
-- now we are creating the strucutre, naming the fields, placing them
-- show the data in the first record as the field name (in case the
-- field names in the first record & to help in remembering the data
-- to assign names to the fields)
repeat with x = 1 to countFields - 1
read from file inFile until tab
delete last character of it
ask "Please type a name for field" && x with it
put it into fieldName
newField fieldName
end repeat
-- this does the same for the last field
read from file inFile until return
ask "Please type a name for field" && countFields with it
put it into fieldName
newfield fieldName
-- all done!
domenu "background"
close file inFile
end createStruct
on importText
global inFile
global error
global countFields
open file inFile
put "dummy" into it
put 1 into recNum
-- loops through the cards & fields until the end of file
-- this usually creates one or two extra cards, but it
-- was easy to code.
put the seconds into startTime
repeat until it is empty
put "Now on record #" & recNum
repeat with x = 1 to countFields -1
read from file inFile until tab
-- this line removes the tab
delete last character of it
repeat while it contains quote
delete char offset(quote,it) of it
end repeat
put it into field x
end repeat
-- now do the same for the last field
read from file inFile until return
put it into field countFields
domenu "new card"
add 1 to recNum
end repeat
-- all done!
put the seconds into stopTime
close file inFile
put stopTime - startTime into timeTook
divide timeTook by 60
hide message window
answer "Done!" && recNum && "records written" with "Ok"
end importText
on goodFile
-- checks to make sure the file exists
-- Hypercard automatically creates a new file when you
-- issue the "open" command. So we read from the file
-- to see if there is anything in it.
-- Be sure to delete any excess files created by this.
-- You can probably type in a path name to a file, if it is not
-- in the same folder as HyperCard
global inFile
global error
open file inFile
read from file inFile until return
if it is empty then
put "That file does not exist"
put "true" into error
close file inFile
exit goodFile
end if
close file inFile
end goodFile
on newField fieldName
-- this routine simply creates the field
-- this lets us add to a background that already contains fields
put the number of background fields into numFields
put numFields + 1 into thisField
put "click where you want the field to go"
wait until the mouseClick
put the mouseLoc into here
domenu "new field"
put first item of here into right
-- fixed current length of field on screen
add 150 to right
put second item of here into bottom
add 20 to bottom
put here && ","&& right && "," && bottom into thePlace
-- these can be changed for different field defaults
set rect of background field thisField to thePlace
set style of background field thisField to rectangle
set name of background field thisField to fieldName
end newField
-- This button is self-contained. That means you can place the button
-- in any background you like & use it to create a stack. It will first
-- create a new stack (with the usual HyperCard "New Stack" dialogue).
-- part 9 (field)
-- low flags: 00
-- high flags: 0007
-- rect: left=58 top=45 right=251 bottom=509
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name:
-- part contents for background part 5
----- text -----
4
-- part contents for background part 12
----- text -----
Text Import
-- part contents for card part 9
----- text -----
IMPORTER 1.1
Copyright © 1987 Stephen Michel
1027 Pomona, Albany, CA 94706 CompuServe: 70611,1215
If you like it, please send $10. Thanx.
ABOUT THIS BUTTON: It will read any TAB DELIMITED file into a new stack, allow you to name fields to hold the data, and do some crude layout of the form. It will provide a relatively complete HyperCard database that includes next, previous, and home buttons -- the rest is up to you.
You will be asked first for the name of the new stack. Next it will want to know what text file file to import - just select it. The script will then read the file, figure out how many fields are in it and ask for the name of each field. Each field name defaults to the data that is in that field in the first record of the file -- to help you remember what each field is, or if you have put the field names at the top, to make it easier.
LIMITATIONS: There are many: 1) As of now, it will only read tab delimited fields, though I am working on other formats (such as mailmerge, comma delimited, SYLK, etc.). 2) All the created fields are the same size on screen. This simplified the coding, and it shouldn't be too hard to go back and adjust them.
CHANGES: Aside from making it more flexible in types of files it can read, it could go faster by locking the screen, so every record doesn't have to get written. But I like to see what is going on. You can also customize this by placing the "import" button onto a background you like better -- it is self contained.
Clicking "cancel" in most dialogues will NOT stop the entire operation -- I haven't done all the error trapping yet. Typing command-period will stop it, though it will leave the source file open. Type "close file" followed by the file name into the message box to close the file. I have worked with files of up to 500 records, and it does take some time (about 10 minutes to read 200 or so records into a stack).